package com.ck.ips.schedule;

import com.ck.core.cache.redis.RedisCacheDao;
import com.ck.core.constant.CacheConstant;
import com.ck.core.lock.LockConstant;
import com.ck.core.lock.RedisDistributeLock;
import com.ck.core.lock.RedisLock;
import com.ck.ips.service.report.StaticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
* @Description: 设备按天需要触发的job
* @Author: hf
* @Date:    2019/5/14 20:46
* @Version: 1.0
*/
@Component
public class DeviceDailyRollingJob {
    private Logger logger = LoggerFactory.getLogger(DeviceDailyRollingJob.class);

    @Autowired
    private RedisCacheDao redisCacheDao;

    @Autowired
    private RedisDistributeLock redisDistributeLock;

    @Autowired
    private StaticsService staticsService;

    /**
     * 每天0点0分0秒执行操作
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void checkProgramStatus() {
        // job加锁，避免在分布式环境多个应用同时执行任务
        try ( RedisLock lock = redisDistributeLock.getLock(LockConstant.JOB_TASK_EXECUTE_LOCK_PREFIX + this.getClass().getName(),
                LockConstant.DEFAULT_TIMEOUT_MILLS, 0, LockConstant.RETRY_INTERVAL_TIME_MILLIS) ) {
            if (null != lock) {

                // 移除设备上报缓存
                redisCacheDao.deleteAll(CacheConstant.CACHE_NAME_CK_DEVICE_ONLINE);

                // 统计汇总上一天在线、离线设备总量
                staticsService.summaryDeviceStatusYesterday();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }


}
